<?php

namespace app\admin\controller;

use app\common\model\station\BabycarStation as BabycarStationModel;

/**
 * TP Babycar 站点管理
 */
class BabycarStation extends Controller
{
    /**
     * 站点列表
     * @return \think\response\Json
     */
    public function list()
    {
        $params = $this->request->param();
        
        // 验证分页参数
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $page = max(1, $page);
        
        // 验证状态参数
        if (isset($params['state']) && !in_array($params['state'], ['active', 'inactive', 'under_maintenance'])) {
            unset($params['state']);
        }
        
        $model = new BabycarStationModel();
        $list = $model->getList($params);
        
        return json(['code' => 0, 'msg' => 'success', 'data' => $list]);
    }

    /**
     * 站点详情
     * @param int $station_id 站点ID
     * @return \think\response\Json
     */
    public function detail($station_id)
    {
        $model = new BabycarStationModel();
        $detail = $model->getDetail($station_id);
        if (!$detail) {
            return json(['code' => 1, 'msg' => '站点不存在']);
        }
        return json(['code' => 0, 'msg' => 'success', 'data' => $detail]);
    }

    /**
     * 创建站点
     * @return \think\response\Json
     */
    public function create()
    {
        $data = $this->request->post();
        // 验证必填字段
        if (empty($data['title']) || empty($data['latitude']) || 
            empty($data['longitude']) || empty($data['manager'])) {
            return json(['code' => 1, 'msg' => '请填写完整站点信息']);
        }
        // 验证经纬度格式
        if (!is_numeric($data['latitude']) || !is_numeric($data['longitude'])) {
            return json(['code' => 1, 'msg' => '经纬度格式不正确']);
        }
        // 验证状态值
        if (isset($data['state']) && !in_array($data['state'], ['active', 'inactive', 'under_maintenance'])) {
            return json(['code' => 1, 'msg' => '状态值不正确']);
        }
        // 检查站点名称是否已存在
        $model = new BabycarStationModel();
        $exists = $model->where('title', $data['title'])->where('is_delete', 0)->find();
        if ($exists) {
            return json(['code' => 1, 'msg' => '站点名称已存在']);
        }
        // 设置默认状态
        if (!isset($data['state'])) {
            $data['state'] = 'active';
        }
        // 设置默认软删除 0代表未删除
        if (!isset($data['is_delete'])) {
            $data['is_delete'] = 0;
        }
        $result = $model->createStation($data);
        if (!$result) {
            return json(['code' => 1, 'msg' => '创建失败']);
        }
        return json(['code' => 0, 'msg' => '创建成功']);
    }

    /**
     * 更新站点
     * @param int $station_id 站点ID
     * @return \think\response\Json
     */
    public function update($station_id)
    {
        $data = $this->request->post();
        
        // 验证经纬度格式
        if (isset($data['latitude']) && !is_numeric($data['latitude'])) {
            return json(['code' => 1, 'msg' => '纬度格式不正确']);
        }
        if (isset($data['longitude']) && !is_numeric($data['longitude'])) {
            return json(['code' => 1, 'msg' => '经度格式不正确']);
        }

        // 验证状态值
        if (isset($data['state']) && !in_array($data['state'], ['active', 'inactive', 'under_maintenance'])) {
            return json(['code' => 1, 'msg' => '状态值不正确']);
        }

        $model = new BabycarStationModel();
        $result = $model->updateStation($station_id, $data);
        if (!$result) {
            return json(['code' => 1, 'msg' => '更新失败']);
        }
        return json(['code' => 0, 'msg' => '更新成功']);
    }

    /**
     * 删除站点
     * @param int $station_id 站点ID
     * @return \think\response\Json
     */
    public function delete($station_id)
    {
        $model = new BabycarStationModel();
        $result = $model->deleteStation($station_id);
        if (!$result) {
            return json(['code' => 1, 'msg' => '删除失败，站点可能不存在或已被删除']);
        }
        return json(['code' => 0, 'msg' => '删除成功']);
    }
} 